﻿using QuanLyDaiLy.Context;
using QuanLyDaiLy.Model;
using QuanLyDaiLy.Platform;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace QuanLyDaiLy.View
{
    /// <summary>
    /// Interaction logic for WDonVi.xaml
    /// </summary>
    public partial class WDonVi : Window
    {
       
        private CDonVi donvi;
        private CDonViContext donviContext;
       
        #region Function

        public WDonVi()
        {
            InitializeComponent();
            InitializeFeature();
        }

        private void InitializeFeature()
        {
            donviContext = new CDonViContext(CConstant.CONNECTION);
            try
            {
                TaoBangDuLieu();
                TrangThaiMacDinh();
                CapNhatDuLieuBang();
            }
            catch { }
        }

        private void TrangThaiMacDinh()
        {
            txtMaDonVi.IsEnabled = false;
            txtTenDonVi.IsEnabled = false;
            btLuu.IsEnabled = false;
            btXoa.IsEnabled = false;
            txtMaDonVi.Clear();
            txtTenDonVi.Clear();
        }

        private void TrangThaiThem()
        {
            txtTenDonVi.IsEnabled = true;
            btLuu.IsEnabled = true;
            btXoa.IsEnabled = false;
            txtTenDonVi.Clear();  
            txtTenDonVi.Focus();
            txtMaDonVi.Text = donviContext.LayMaSoMoi().ToString();
        }

        private void TrangThaiChon()
        {
            btLuu.IsEnabled = true;
            btXoa.IsEnabled = true;
            txtTenDonVi.IsEnabled = true;
        }

        private bool KiemTraNhap()
        {
            if (txtTenDonVi.Text == "")
            {
                MessageBox.Show("Vui Lòng Nhập Tên Quận");
                txtTenDonVi.Focus();
                return false;
            }

            return true;
        }

        private void LuuDonVi()
        {
            try
            {
                if (KiemTraNhap())
                {
                    donvi = new CDonVi(int.Parse(txtMaDonVi.Text) ,txtTenDonVi.Text);
                    if (Convert.ToInt32(txtMaDonVi.Text) < donviContext.LayMaSoMoi())
                    {
                        var result = MessageBox.Show("Bạn Có Chắc Chắn Sửa Không?", "Thông Báo", MessageBoxButton.YesNo, MessageBoxImage.Question);
                        if (result == MessageBoxResult.Yes)
                        {
                            donviContext.CapNhat(donvi);
                            MessageBox.Show("Sửa Thành Công");
                        }
                        TrangThaiChon();
                    }
                    else
                    {
                        donviContext.Them(donvi);
                        MessageBox.Show("Lưu Thành Công");
                        TrangThaiThem();
                    }

                    CapNhatDuLieuBang();
                }
            }
            catch { }
                
        }

        private void XoaDonVi()
        {
            try
            {
                var result = MessageBox.Show("Bạn Có Chắc Chắn Xóa Không?", "Thông Báo", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {
                    donviContext.Xoa(Convert.ToInt32(txtMaDonVi.Text));
                    CapNhatDuLieuBang();
                    TrangThaiMacDinh();
                    MessageBox.Show("Xóa Thành Công");                 
                }
            }
            catch { MessageBox.Show("Chưa Thể Xóa"); }
        }

        private void ChonDonVi()
        {
            try
            {
                TrangThaiChon();
                if (lvDonVi.SelectedItem is CDonVi)
                {
                    txtMaDonVi.Text = (lvDonVi.SelectedItem as CDonVi).MaDonVi.ToString();
                    txtTenDonVi.Text = (lvDonVi.SelectedItem as CDonVi).TenDonVi;
                }
            }
            catch { }
        }

        private void TaoBangDuLieu()
        {
            try
            {
                GridView grdView = new GridView();
                grdView.AllowsColumnReorder = true;
                grdView.ColumnHeaderToolTip = "DONVI";

                GridViewColumn maquan = new GridViewColumn();
                maquan.DisplayMemberBinding = new Binding("MaDonVi");
                maquan.Header = "Mã Đơn Vị";
                maquan.Width = 120;
                grdView.Columns.Add(maquan);

                GridViewColumn tenquan = new GridViewColumn();
                tenquan.DisplayMemberBinding = new Binding("TenDonVi");
                tenquan.Header = "Tên Đơn Vị";
                tenquan.Width = 120;
                grdView.Columns.Add(tenquan);

                lvDonVi.View = grdView;
            }
            catch { }
        }

        private void CapNhatDuLieuBang()
        {
            lvDonVi.ItemsSource = donviContext.LayDanhSachTatCa();
        }

        #endregion

      
       

        #region EVEN

        private void btThemDonVi_Click(object sender, RoutedEventArgs e)
        {
            TrangThaiThem();
        }

        private void btLuu_Click(object sender, RoutedEventArgs e)
        {
            LuuDonVi();
        }

        private void btXoa_Click(object sender, RoutedEventArgs e)
        {
            XoaDonVi();
        }

        private void txtTenDonVi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                LuuDonVi();
            }
        }

        private void lvDonVi_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ChonDonVi();
        }


        #endregion

        

    }
}
